package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.SameListModel;
import com.jiayang.hhotel.presenter.AddedSpecialRoomStatePresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.jiayang.hhotel.view.DateChooseWheelViewDialog;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/6/6 0006.
 * 添加特殊房态页面
 */

public class AddedSpecialRoomStateActivity extends XActivity<AddedSpecialRoomStatePresenter> implements View.OnClickListener, DateUtil.PickerDeterListener, RadioGroup.OnCheckedChangeListener {

    private TextView cause;//选择原因
    private TextView StartDate,EndDate;//开始，结束日期选择
    private RadioGroup rg;
    private EditText input;

    private Button save;

    private String startDateStr,endDateStr;//开始日期，结束日期字符串
    private int index = -1;//日期选择下标（1:开始日期;2:结束日期）
    private int type = 1;//类型:1,维修房；2，非出租房；3，锁房
    private SameListModel.CceDtoListBean causeModel;//原因对象(用此对象判断是否更换或者选择了类型)

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.special_room_state),R.color.whitetext,null,null);

        StartDate = (TextView) findViewById(R.id.tv_sel_start_date);
        EndDate = (TextView) findViewById(R.id.tv_sel_end_date);
        cause = (TextView) findViewById(R.id.tv_sel_cause);

        rg = (RadioGroup) findViewById(R.id.rg);
        input = (EditText) findViewById(R.id.et_num);

        save = (Button) findViewById(R.id.bt_save);

        StartDate.setOnClickListener(this);
        EndDate.setOnClickListener(this);
        cause.setOnClickListener(this);
        save.setOnClickListener(this);
        rg.setOnCheckedChangeListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_added_special_room_state;
    }

    @Override
    public AddedSpecialRoomStatePresenter newP() {
        return new AddedSpecialRoomStatePresenter();
    }

    private Intent intent;
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_sel_cause://选择原因
                intent = new Intent(this,SameActivity.class);
                intent.putExtra(Constant.INTENT_STRING_KEY,"特殊房态原因");
                intent.putExtra(Constant.INTENT_INT_KEY,type);
                startActivityForResult(intent,Constant.GUEST_ROOM_THING_REQUEST_CODE);
                break;
            case R.id.tv_sel_start_date://选择开始日期
//                DateUtil.initDatePicker(this,this);
                DateChooseWheelViewDialog startDateChooseDialog = new DateChooseWheelViewDialog(AddedSpecialRoomStateActivity.this, new DateChooseWheelViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        startDateStr = time;
                        StartDate.setText(startDateStr);
                        index = 1;
                    }
                });
                startDateChooseDialog.setDateDialogTitle(getString(R.string.start_date));
                startDateChooseDialog.showDateChooseDialog();
                break;
            case R.id.tv_sel_end_date://结束日期
//                DateUtil.initDatePicker(this,this);
                final DateChooseWheelViewDialog endDateChooseDialog = new DateChooseWheelViewDialog(AddedSpecialRoomStateActivity.this, new DateChooseWheelViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        endDateStr = time;
                        EndDate.setText(endDateStr);
                        index = 2;
                    }
                });
                endDateChooseDialog.setDateDialogTitle(getString(R.string.end_date));
                endDateChooseDialog.showDateChooseDialog();
                break;
            case R.id.bt_save://保存
                String roomNum = input.getText().toString();
                if(StringUtil.isEmpty(roomNum)) {
                    ToastUtil.Toast(getString(R.string.please_input_room_num), this);
                }else if(StringUtil.isEmpty(startDateStr)) {
                    ToastUtil.Toast(getString(R.string.please_sel_start_date_section), this);
                }else if(StringUtil.isEmpty(endDateStr)){
                    ToastUtil.Toast(getString(R.string.please_sel_end_date_section), this);
                }else if(causeModel == null){
                    ToastUtil.Toast(getString(R.string.please_sel_cause), this);
                }else{
                    //todo  保存
                    CommonUtil.showProgressDialog(this,R.string.loading);
                    getP().cancel(Constant.LAUGUAGE_FLAG_CHINESE,1,roomNum,causeModel.getCode()
                    ,startDateStr.split(" ")[0],startDateStr.split(" ")[1],endDateStr.split(" ")[0]
                    ,endDateStr.split(" ")[0]);
                }
                break;
        }
    }

    @Override
    public void onPicker(String date) {
        switch (index){
            case 1:
                StartDate.setText(date);
                break;
            case 2:
                EndDate.setText(date);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i){
            case R.id.rb_repair://维修房
                if(type != 1){
                    causeModel = null;
                    cause.setText(getString(R.string.click_select));
                }
                type = 1;
                break;
            case R.id.rb_un_lease_room://非出租房
                if(type != 2){
                    causeModel = null;
                    cause.setText(getString(R.string.click_select));
                }
                type = 2;
                break;
            case R.id.rb_un_conference://锁房
                if(type != 3){
                    causeModel = null;
                    cause.setText(getString(R.string.click_select));
                }
                type = 3;
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constant.GUEST_ROOM_THING_REQUEST_CODE && resultCode == Constant.GUEST_ROOM_THING_RESULT_CODE){
            causeModel = (SameListModel.CceDtoListBean) data.getExtras().getSerializable(Constant.INTENT_PROJECT_KEY);
            if(causeModel != null){
                cause.setText(causeModel.getChnDescp());
            }
        }
    }
}
